//
//  ChangePwdViewController.m
//  SleepCare500
//
//  Created by bchy on 2019/8/8.
//  Copyright © 2019 BoChuang. All rights reserved.
//

#import "ChangePwdViewController.h"

@interface ChangePwdViewController ()

@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak, nonatomic) IBOutlet UITextField *textField3;

@property (nonatomic, strong) UILabel *tipLabel1;
@property (nonatomic, strong) UILabel *tipLabel2;
@property (nonatomic, strong) UILabel *tipLabel3;

@end

@implementation ChangePwdViewController

-(UILabel *)tipLabel1{
    if (!_tipLabel1) {
        _tipLabel1 = [UILabel new];
        _tipLabel1.text = @"请输入旧密码";
        _tipLabel1.font = [UIFont systemFontOfSize:15];
        _tipLabel1.textColor = [UIColor colorWithHexString:@"#B7BCC6" andAlpha:1];
    }
    return _tipLabel1;
}

-(UILabel *)tipLabel2{
    if (!_tipLabel2) {
        _tipLabel2 = [UILabel new];
        _tipLabel2.text = @"请输入新密码(不少于6位)";
        _tipLabel2.font = [UIFont systemFontOfSize:15];
        _tipLabel2.textColor = [UIColor colorWithHexString:@"#B7BCC6" andAlpha:1];
    }
    return _tipLabel2;
}
-(UILabel *)tipLabel3{
    if (!_tipLabel3) {
        _tipLabel3 = [UILabel new];
        _tipLabel3.text = @"请确认您的新密码(不少于6位)";
        _tipLabel3.font = [UIFont systemFontOfSize:15];
        _tipLabel3.textColor = [UIColor colorWithHexString:@"#B7BCC6" andAlpha:1];
    }
    return _tipLabel3;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改密码";

    _submitBtn.backgroundColor = [UIColor colorWithHexString:@"#DCDFE6" andAlpha:1];
    _submitBtn.layer.cornerRadius = 22;
    _submitBtn.layer.shadowOffset = CGSizeMake(2, 2);
    _submitBtn.layer.shadowColor = THEMECOLOR.CGColor;
    _submitBtn.layer.shadowOpacity = 0;
    
    [self.textField1 addTarget:self action:@selector(textFieldStartEdit:) forControlEvents:UIControlEventEditingDidBegin];
    [self.textField2 addTarget:self action:@selector(textFieldStartEdit:) forControlEvents:UIControlEventEditingDidBegin];
    [self.textField3 addTarget:self action:@selector(textFieldStartEdit:) forControlEvents:UIControlEventEditingDidBegin];
    [self.textField1 addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];
    [self.textField2 addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];
    [self.textField3 addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];
    
    [self.textField1 addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
    [self.textField2 addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
    [self.textField3 addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
    
    [self.view addSubview:self.tipLabel1];
    [self.tipLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(40);
        make.centerY.equalTo(self.textField1);
    }];
    [self.view addSubview:self.tipLabel2];
    [self.tipLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(40);
        make.centerY.equalTo(self.textField2);
    }];
    [self.view addSubview:self.tipLabel3];
    [self.tipLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(40);
        make.centerY.equalTo(self.textField3);
    }];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
    
}

-(void)textFieldStartEdit:(UITextField *)textField{
    if (textField.text.length > 0) {
        return;
    }
    if (textField == _textField1) {
        
        [self.tipLabel1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.textField1).mas_offset(-26);
        }];
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
    if (textField == _textField2) {
        
        [self.tipLabel2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.textField2).mas_offset(-26);
        }];
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
    if (textField == _textField3) {
        
        [self.tipLabel3 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.textField3).mas_offset(-26);
        }];
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
}

-(void)textFieldEndEdit:(UITextField *)textField{
    if (textField.text.length > 0) {
        return;
    }
    if (textField == _textField1) {
        [self.tipLabel1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.textField1);
        }];
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
    if (textField == _textField2) {
        [self.tipLabel2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.textField2);
        }];
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
    if (textField == _textField3) {
        [self.tipLabel3 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.textField3);
        }];
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
}

-(void)textFieldEditing:(UITextField *)textField{
    if (_textField1.text.length > 0 && _textField2.text.length > 5 && _textField3.text.length > 5) {
        _submitBtn.userInteractionEnabled = YES;
        _submitBtn.layer.shadowOpacity = 0.5;
        _submitBtn.backgroundColor = THEMECOLOR;
    }else{
        _submitBtn.userInteractionEnabled = NO;
        _submitBtn.layer.shadowOpacity = 0;
        _submitBtn.backgroundColor = [UIColor colorWithHexString:@"#DCDFE6" andAlpha:1];
    }
}
#pragma mark - 修改
- (IBAction)submit:(id)sender {
    [self.view endEditing:YES];
    if (![_textField2.text isEqualToString:_textField3.text]) {
        [MBProgressHUD showError:@"两次密码不一致" toView:self.view];
        return;
    }
    NSDictionary *dict = @{
        @"newPassword": _textField2.text,
        @"oldPassword": _textField1.text,
        @"userId": [User sharedUser].userId
    };
//    [NetWork sendPostUrl:@"person/changePassword" withParams:dict success:^(id responseBody) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        [MBProgressHUD showSuccess:@"修改成功" toView:nil];
//        [self.navigationController popViewControllerAnimated:YES];
//    } failure:^(NSError *error) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//    }];
}

-(void)tap{
    [self.view endEditing:YES];
}

@end
